package Class_test2;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class MyReflect {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
       //获取class字节码文件
        Class clazz=Class.forName("Class_test1.Student");
        //获取所有成员变量
        Field[] files=clazz.getDeclaredFields();
        for (Field file : files) {
            System.out.println(file);
        }
        //获取单个成员变量
        Field name=clazz.getDeclaredField("name");
        System.out.println(name);
        System.out.println(name.getName());
        //获取权限修饰符
        int modifiers=name.getModifiers();
        System.out.println(modifiers);
        //获取数据类型
        Class<?> type=name.getType();
        System.out.println(type);
        //获取成员变量记录的值
        Student stu=new Student("张三",18);
        name.setAccessible(true);
        String value=(String) name.get(stu);
        System.out.println(value);
        //设置值
        name.set(stu,"李四");
        System.out.println(stu);
    }
}
